<?php
!defined('P_W') && exit('Forbidden');

class PW_HouseInfoDB extends BaseDB {

	var $_tableName = "pw_house_info";
	var $_tableName_extra = 'pw_house_infoextra';
	
	function getInfoById($id) {
		return $this->_db->get_one('SELECT * FROM ' . $this->_tableName . ' WHERE hid=' . pwEscape($id));
	}

	function getAllInfoById($id) {
		return $this->_db->get_one('SELECT a.*,b.* FROM ' . $this->_tableName . ' a LEFT JOIN ' . $this->_tableName_extra . ' b ON a.hid=b.hid WHERE a.hid=' . pwEscape($id));
	}

	function getListByIds($ids) {
		$query = $this->_db->query('SELECT * FROM ' . $this->_tableName . ' WHERE hid IN(' . pwImplode($ids) . ')');
		return $this->_getAllResultFromQuery($query);
	}

	function searchList($where, $offset = 0, $limit = 0, $order = 0) {
		$_sql_where		= $this->sqlCompound($where);
		$_sql_orderby	= $order ? 'aveprice ' . (($order == 2) ? 'DESC' : 'ASC') : 'hid DESC';
		$_sql_limit		= $limit ? pwLimit($offset, $limit) : '';
		$array = array();
		$query = $this->_db->query('SELECT * FROM ' . $this->_tableName . " WHERE 1{$_sql_where} ORDER BY $_sql_orderby $_sql_limit");
		while ($rt = $this->_db->fetch_array($query)) {
			$array[$rt['hid']] = $rt;
		}
		return $array;
	}

	function getCount($where) {
		$_sql_where		= $this->sqlCompound($where);
		return $this->_db->get_value('SELECT COUNT(*) AS sum FROM ' . $this->_tableName . " WHERE 1{$_sql_where}");
	}

	function updateByIds($pwSQL, $ids) {
		$this->_db->update('UPDATE ' . $this->_tableName . ' SET ' . pwSqlSingle($pwSQL) . ' WHERE hid IN(' . pwImplode($ids) . ')');
	}

	function updateHits($id) {
		$this->_db->update('UPDATE ' . $this->_tableName . " SET hits=hits+'1'" . ' WHERE hid=' . pwEscape($id));
	}
	
	function updateCollectnum($id) {
		$this->_db->update('UPDATE ' . $this->_tableName . " SET collectnum=collectnum+'1'" . ' WHERE hid=' . pwEscape($id));
	}

	function deleteByIds($ids) {
		$this->_db->update('DELETE FROM ' . $this->_tableName . ' WHERE hid IN(' . pwImplode($ids) . ')');
		$this->_db->update('DELETE FROM ' . $this->_tableName_extra . ' WHERE hid IN(' . pwImplode($ids) . ')');
	}

	function getByMultiField($field, $value) {
		return $this->_db->get_one('SELECT * FROM pw_house_info WHERE ' . intval($value) . "&$field LIMIT 1");
	}

	function getBySingleField($field, $value) {
		return $this->_db->get_one("SELECT * FROM pw_house_info WHERE $field IN(" . pwImplode($value) . ") LIMIT 1");
	}

	function sqlIn($ids) {
		return (is_array($ids) && $ids) ? ' IN (' . pwImplode($ids) . ')' : '=' . pwEscape($ids);
	}

	function sqlCompound($where) {
		$_sql_where = '';
		foreach ($where as $_sql_field => $value) {
			switch ($_sql_field) {
				case 'area':
				case 'plate':
				case 'status':
				case 'ifopen':
				case 'housetype':
				case 'developer':
					$_sql_where .= " AND $_sql_field" . $this->sqlIn($value);break;
				case 'property':
				case 'buildtype':
				case 'paystatus':
					$bitv = 0;
					if (is_array($value)) {
						foreach ($value as $k => $v) {
							$v > 0 && $bitv += 1 << (intval($v)-1);
						}
					} else {
						$bitv = $value > 1 ? (1 << (intval($value)-1)) : 1;
					}
					$_sql_where .= " AND $_sql_field&$bitv";break;
				case 'deliverdate':
				case 'opendate':
					if ($value['start']) {
						$_sql_where .= " AND $_sql_field>=" . pwEscape($value['start']);
					}
					if ($value['end']) {
						$_sql_where .= " AND $_sql_field<=" . pwEscape($value['end']);
					}
					break;
				case 'aveprice':
					global $house_field;
					if (is_array($value)) {
						$_sql_arr = array();
						foreach ($value as $k => $v) {
							if (!isset($house_field['aveprice'][$v])) continue;
							$_sql_sub = $_sql_flag = '';
							if ($house_field['aveprice'][$v]['start']) {
								$_sql_sub .= "$_sql_field>=" . pwEscape($house_field['aveprice'][$v]['start']);
								$_sql_flag = ' AND ';
							}
							if ($house_field['aveprice'][$v]['end']) {
								$_sql_sub .= $_sql_flag . "$_sql_field<=" . pwEscape($house_field['aveprice'][$v]['end']);
							}
							$_sql_sub && $_sql_arr[$v] = '(' . $_sql_sub . ')';
						}
						if ($_sql_arr) {
							$_sql_where .= ' AND (' . implode(' OR ', $_sql_arr) . ')';
						}
					} else {
						if ($house_field['aveprice'][$value]['start']) {
							$_sql_where .= " AND $_sql_field>=" . pwEscape($house_field['aveprice'][$value]['start']);
						}
						if ($house_field['aveprice'][$value]['end']) {
							$_sql_where .= " AND $_sql_field<=" . pwEscape($house_field['aveprice'][$value]['end']);
						}
					}
					break;
				case 'name':
//					$_sql_where .= " AND name LIKE " . pwEscape(str_replace('*','%',$value));
					$_sql_where .= " AND name LIKE '%".$value."%'";
					break;
			}
		}
		return $_sql_where;
	}
}
?>